package leetcode.offer0064;

import java.util.HashSet;
import java.util.Set;

public class MagicDictionary {

	Set<String> set;

	public MagicDictionary() {
		set = new HashSet<>();
	}

	public void buildDict(String[] dictionary) {
		for (String dict : dictionary) {
			set.add(dict);
		}
	}

	public boolean search(String searchWord) {
		char[] chars = searchWord.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			for (int j = 0; j < 26; j++) {
				chars[i] = (char) (j + 'a');
				if (chars[i] == c) {
					continue;
				}
				if (set.contains(new String(chars))) {
					return true;
				}
			}
			chars[i] = c;
		}
		return false;
	}

}
